package util;

import util.exceptions.UnknownElementException;

public class MoleculeComponent {
    private Atom element;
    private int multiplicity;
    private boolean electron;
    /**
     * Creates a new MoleculeComponent.
     *
     * @param elementName  name of the element
     * @param multiplicity the number of elements
     * @throws UnknownElementException if the exception does not exist
     */
    public MoleculeComponent(String elementName, int multiplicity) throws UnknownElementException {
        this.multiplicity = multiplicity;

        element = Periodic.elements.get(elementName);
        if (element==null && !elementName.replaceAll(" ", "").equals(""))
        	throw new UnknownElementException(elementName);
    }

    /**
     * Gets the element of the component
     *
     * @return The element
     */
    public Atom getElement() {
        return element;
    }

    /**
     * Gets the number of atoms
     *
     * @return the multiplicity of the element
     */
    public int getMultiplicity() {
        return multiplicity;
    }

    @Override
    public boolean equals(Object o) {
        if(!(o instanceof MoleculeComponent))
            return false;
        if(element == null && ((MoleculeComponent) o).getElement() == null)
            return true;
        return ((MoleculeComponent) o).getElement().equals(element) && ((MoleculeComponent) o).getMultiplicity() == multiplicity;
    }

    public String toString() {
        if (multiplicity != 1)
            return element.toString() + multiplicity;
        return element.toString();
    }

    public boolean isElectron() {
        return electron;
    }
}
